package com.wxj.book.controller;

import com.wxj.book.common.Const;
import com.wxj.book.common.ServerResponse;
import com.wxj.book.pojo.Comment;
import com.wxj.book.pojo.User;
import com.wxj.book.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * 添加评论
     */
    @PostMapping("comment/addComment")
    @ResponseBody
    public ServerResponse addComment(Comment comment, HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("添加失败，请先登录");
        }
        int result = commentService.addComment(comment, user.getStuNum());
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }
}
